package com.dq.graph;
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.ST;

public class SymbolGraph {
	private ST<String,Integer> st ;	//符号名-->索引
	private String []keys;			//索引-->符号名
	private Graph G;
	public SymbolGraph(String stream,String sp)
	{
		st = new ST<String, Integer>();
		In in = new In(stream);
		while(in.hasNextLine())
		{
			String []a = in.readLine().split(sp);
			for(int i=0;i<a.length;i++)
			{
				if(!st.contains(a[i]))
				{
					st.put(a[i], st.size());
				}
			}
		}
		keys = new String[st.size()];
		for(String key:st.keys())
			keys[st.get(key)] = key;
		in = new In(stream);
		G = new Graph(st.size());
		while(in.hasNextLine())
		{
			String []a = in.readLine().split(sp);
			int v = st.get(a[0]);
			for(int i=1;i<a.length;i++)
				G.addEdge(v, st.get(a[i]));
		}
	}
	public String[] keys()
	{	return keys;}
	public boolean contains(String s)
	{	return st.contains(s);		}
	public int index(String s)
	{	return st.get(s);			}
	public String name(int v)
	{	return keys[v];				}
	public Graph G()
	{	return G;					}
}
